package com.apes.fn.fico.debitnote.repository;

import com.apes.fn.fico.debitnote.model.BusinessDebitNote;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * param:zyc
 **/
public interface BusinessDebitNoteRepository extends CustomRepository<BusinessDebitNote, String> {

    @Query("select T from BusinessDebitNote  T where T.reason like %:reason%  ")
    BusinessDebitNote findByReasonLike(String reason);

    BusinessDebitNote findBysaleDeptIdAndState(String fnCustomerId,String state);

    List<BusinessDebitNote> findAllByApproveDateIsNotNullAndStateNotIn(String state);

    /**
     * fetch data by date and fnCustomer id and state
     *
     * @param fnCustomerId     fnCustomer id
     * @return List<BusinessDebitNote>
     */
    @Query("select T from BusinessDebitNote  T where T.fnCustomer.id=?1 and (T.withhold in('unwithheld','partialDeduction')) " +
            "and (T.state not in('create','apply','delete')) and T.capitalBalance <> T.unBuckledBudgetAmount ")
    List<BusinessDebitNote> findAllByFnCustomerId(String fnCustomerId);


    /**
     *
     * @param fnCustomerId
     * @param startDate
     * @return
     */
    @Query("select T from BusinessDebitNote  T where T.limitDate <= ?2 and T.fnCustomer.id=?1   and (T.withhold in('unwithheld','partialDeduction')) " +
            "and (T.state not in('create','apply','delete')) and T.capitalBalance <> T.unBuckledBudgetAmount ")
    List<BusinessDebitNote> findAllByFnCustomerId(String fnCustomerId,Date startDate);


    /**
     * fetch data by date and fnCustomer id and state
     *
     * @param fnCustomerId     fnCustomer id
     * @param startDate     approveDate
     * @param endDate     approveDate
     * @param pageable     page
     * @return List<BusinessDebitNote>
     */
    @Query("select T from BusinessDebitNote  T where T.fnCustomer.id=?1 and (T.state in('pending','arrangement','pay','paid')) " +
            " and T.approveDate>=?2 and T.approveDate<?3")
    Page<BusinessDebitNote> findAllByFnCustomerId(String fnCustomerId, Date startDate, Date endDate, Pageable pageable);

    /**
     * fetch data by date and fnCustomer id and state
     *
     * @param fnCustomerId     fnCustomer id
     * @param State     State
     * @return List<BusinessDebitNote>
     */
    @Query("select T from BusinessDebitNote  T where T.fnCustomer.id=?1 and T.state=?2 and T.loan=false ")
    List<BusinessDebitNote> findAllByFnCustomerIdAndState(String fnCustomerId,String State);

    /**
     * fetch data by date and fnCustomer id and state
     *
     * @param fnCustomerId     fnCustomer id
     * @param date     date
     * @return List<BusinessDebitNote>
     */
    @Query("select T from BusinessDebitNote  T where T.fnCustomer.id=?1 and (T.state ='approve' or T.state='partialDeduction') and T.createDate<=?2")
    List<BusinessDebitNote> findAllByFnCustomerIdAndCreateDate(String fnCustomerId, Date date);

    @Query("SELECT A.id as id FROM BusinessDebitNote A WHERE A.id = :id")
    Map<String, Object> findIdAndName(@Param("id") String orderId);
}
